﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using libs.Model;
using libs.DAL;
using libs.DBUtility;

namespace Sever
{
    /// <summary>
    /// SwitchsHandler 的摘要说明
    /// </summary>
    public class SwitchsHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["type"];
            int qid = int.Parse(context.Request["id"]);
            userDao userd = new userDao();
            if (!userd.Exists(qid))
            {
                context.Response.Write("{\"result\":\"error\"}");
            }
            else
            {
                user uuu = userd.GetModel("id=" + qid);
                if (uuu.enable == false)
                {
                    context.Response.Write("{\"result\":\"error\"}");
                }
                else
                {
                    switchsDao dao = new switchsDao();
                    if (!string.IsNullOrEmpty(type))
                    {
                        switch (type)
                        {
                                //开关
                            case "Switch":
                                string json = context.Request["switch"];
                                string jsonlog = context.Request["log"];
                                int ZT = dao.getZT(json);
                                int nn = ZT == 0 ? 1 : 0;
                                bool res = dao.Update(json + "=" + nn);
                                if (res)
                                {
                                    if (!jsonlog.Equals("no")) {
                                        log log = JsonHelper.DeserializeJsonToObject<log>(jsonlog);
                                        logDao logd = new logDao();
                                        logd.Add(log);
                                    }
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                                //查询参数
                            case "Select":
                                switchs models = dao.GetModel();
                                string jj = JsonHelper.SerializeObject(models);
                                context.Response.Write("{\"result\":" + jj + "}");
                                break;
                        }
                    }
                }
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}